package ThreadDemo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-08
 * Time: 14:14
 */
class Test {
    public int count = 0;

    //java中 synchronized 是可重入的
    synchronized public void func() {
        synchronized (this) {
            count++;
        }
    }

    public void Add(){
        count++;
    }
}

public class ThreadDemo12 {
    public static void main(String[] args) {
        Test test = new Test();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                test.func();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                test.Add();
            }
        });

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("count: " + test.count);

    }
}
